package com.ruoyi.quartz.task;

import cn.hutool.http.HttpUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.model.domain.SelectPush;
import com.ruoyi.system.model.mapper.*;
import com.ruoyi.system.outto.service.IOutApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
public class HuaWeiTask {

    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private RedisCache redisCache;

    static String hwTokenxyr = "hwTokenxyr";

//    @Scheduled(cron = "0/5 * * * * ?")
    //定时获取华为Token
    private void  huaweiToken() {

        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByType("huaweiyun");

//        String userr = "user123";
//        String pwd = "qwer..0011";
//        String fname = "hid_0uks7w_0pib40z7";
//        String domain = "cn-north-4";
        String userr = "";
        String pwd = "";
        String fname = "";
        String domain = "";
        for (SysDictData sysDictData : sysDictDataList) {
            if (sysDictData.getDictLabel().equals("username")){
                userr = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("pwd")){
                pwd = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("fname")){
                fname = sysDictData.getDictValue();
            }
            if (sysDictData.getDictLabel().equals("domain")){
                domain = sysDictData.getDictValue();
            }
        }

        String url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens";
        String body = "{\n" +
                "  \"auth\": {\n" +
                "    \"identity\": {\n" +
                "      \"methods\": [\n" +
                "        \"password\"\n" +
                "      ],\n" +
                "      \"password\": {\n" +
                "        \"user\": {\n" +
                "          \"domain\": {\n" +
                "            \"name\": \"fname\"\n" +
                "          },\n" +
                "          \"name\": \"userr\",\n" +
                "          \"password\": \"pwd\"\n" +
                "        }\n" +
                "      }\n" +
                "    },\n" +
                "    \"scope\": {\n" +
                "      \"project\": {\n" +
                "        \"name\": \"domainStr\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        body = body.replace("fname",fname).replace("userr",userr).replace("pwd",pwd).replace("domainStr",domain);
        try {
            System.out.println(body);
            String token = HttpUtils.sendHWPost(url, body, new HashMap<>());
            redisCache.setCacheObject(hwTokenxyr,token);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}